package com.iotsz.device.service.impl;

import com.iotsz.device.bean.Device;
import com.iotsz.device.feign.UserFeignService;
import com.iotsz.device.mapper.DeviceMapper;
import com.iotsz.device.service.DeviceService;
import com.iotsz.user.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private UserFeignService userFeignService;

    @Override
    public Device getDevice(Integer id) {
        return deviceMapper.getDevice(id);
    }

    @Override
    public Map<Device, User> getDeviceUser(Integer id) {
        Device device = deviceMapper.getDevice(id);
        User user = userFeignService.getDeviceUser(device.getUserId());
        Map<Device, User> res = new HashMap<>();
        res.put(device, user);
        return res;
    }

    public int updateByDevice(@RequestBody Device device) {
        return deviceMapper.updateByDevice(device);
    }

    public int deleteByPrimary(Integer id) {
        return deviceMapper.deleteByPrimary(id);
    }

    public int insertByDevice(@RequestBody Device device) {

        return deviceMapper.insertByDevice(device);
    }
}
